<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\PurchaseGoodsOut as M;
use app\common\validate\PurchaseGoodsOut as V;

class PurchaseGoodsOut
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            $admin = session('admin');
            $data['admin_id'] = $admin['id'];
            $data['apply_id'] = 0;
            $data['apply_info'] = '';
            $data['school_id'] = $admin['school_id'];
            $data['add_time'] = time();
            M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    public static function goodsBack($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            \app\common\model\PurchaseGoods::where('id', $model['goods_id'])->inc('goods_stock', $model['goods_stock'])->update();

            $model->save([
                'back_time' =>toDate( time()),
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    /**\
     * 出库通过审核
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function applyPassDo($data)
    {
        $model =  M::find($data['id']);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $goods = \app\common\model\PurchaseGoods::where(['id' => $model['goods_id']])->find();
            if ($model['goods_stock'] > $goods['goods_stock']) {
                return ['msg'=>'库存不足','code'=>201];
            }
            dataDec('bl_purchase_goods', ['id' => $model['goods_id']], 'goods_stock', $model['goods_stock']);
            $admin = session('admin');
            $model->save([
                'apply_info' => $data['apply_info'],
                'apply_time' => time(),
                'status' => 2,
                'apply_id' => $admin['id'],
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    /**
     * 拒绝审核
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function applyRefuseDo($data)
    {
        $model =  M::find($data['id']);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $admin = session('admin');
            $model->save([
                'apply_info' => $data['apply_info'],
                'apply_time' => time(),
                'status' => 3,
                'apply_id' => $admin['id'],
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
}
